Conditions | 2 |
Paths | 2 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict'; |
||
55 | .end(function (err, ires) { |
||
56 | if (err) { |
||
57 | console.log((timeStamp() + 'Failed to get the Cookie.\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
58 | res.send({ error: '获取Cookie失败' }); |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | // 登录POST请求的headers,是我抓包来的 |
||
63 | let headers = { |
||
64 | Host: 'csujwc.its.csu.edu.cn', |
||
65 | Connection: 'keep-alive', |
||
66 | 'Cache-Control': 'max-age=0', |
||
67 | Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
||
68 | Origin: 'http://csujwc.its.csu.edu.cn', |
||
69 | 'Upgrade-Insecure-Requests': 1, |
||
70 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', |
||
71 | 'Content-Type': 'application/x-www-form-urlencoded', |
||
72 | Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/', |
||
73 | 'Accept-Encoding': 'gzip, deflate', |
||
74 | 'Accept-Language': 'zh-CN,zh;q=0.8', |
||
75 | Cookie: ires.headers['set-cookie'] // 猜测:感觉这个cookie也是不变的,不过出于稳定性,还是动态获取了 |
||
76 | }; |
||
77 | |||
78 | let account = encodeInp(id); |
||
79 | let passwd = encodeInp(pwd); |
||
80 | let encoded = escaper.escape(account + "%%%" + passwd); |
||
81 | |||
82 | // POST登录 |
||
83 | superagent |
||
84 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk') |
||
85 | .set(headers) |
||
86 | .type('form') |
||
87 | .send({ encoded: encoded }) |
||
88 | .end(function (err, iires) { |
||
89 | // 如果登录信息正确,这里是对http://csujwc.its.csu.edu.cn/jsxsd/framework/xsMain.jsp的GET请求 |
||
90 | // 如果错误,会变成对http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk的POST请求 |
||
91 | if (err) { |
||
92 | console.log((timeStamp() + 'Failed to login\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
93 | res.send({ error: '登录失败' }); |
||
94 | return; |
||
95 | } |
||
96 | if (/POST/i.test(iires.req.method)) { |
||
97 | console.log((timeStamp() + 'Failed to login\nPossibily id or password provided were wrong').red); |
||
98 | res.send({ error: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' }); |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | // 将相应的headers和返回的response(刚进去的首页)传入callback |
||
103 | callback(headers, iires); |
||
104 | }); |
||
105 | }); |
||
106 | }; |
||
125 |